package com.xm.xmapp.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.app.RequestInterFaceCode.SeekCarCode;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.PasswordDialog;
import com.xm.xmapp.customview.TimerTaskTextView;
import com.xm.xmapp.customview.TimerTaskTextView.StopTimerTask;
import com.xm.xmapp.dialog.TipLeftRightDialog;
import com.xm.xmapp.myinterface.DialogInterFace;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MainActivityInterFace;
import com.xm.xmapp.request.bean.Big_customer_rent_carRequest;
import com.xm.xmapp.request.bean.Cancel_subscribeRequest;
import com.xm.xmapp.request.bean.Seek_carRequest;
import com.xm.xmapp.request.bean.Seek_carResponse;
import com.xm.xmapp.response.bean.BigCustomerOrderBean;
import com.xm.xmapp.response.bean.CustomHeightBean;
import com.xm.xmapp.response.bean.DaKeHuYuYueBean;
import com.xm.xmapp.response.bean.InterFaceBigCustomerOrderBeanAble;
import com.xm.xmapp.tools.Des4;

public class BigCustomerSubscribeFragment extends BaseFragment implements OnClickListener {

	private DaKeHuYuYueBean bean;
	private Dialog mydialog;
	private TextView name, quxiao, xunche;
	private TimerTaskTextView time_text;
	private LinearLayout framelayout;

	public BigCustomerSubscribeFragment() {
		super();
	}

	public void setData(InterFaceBigCustomerOrderBeanAble bean) {
		if (bean == null) {
			this.bean = null;
			if (time_text != null) {
				time_text.stopTimerTask();
			}
		} else {
			this.bean = (DaKeHuYuYueBean) bean;
			if (view != null && this.bean != null) {
				initData();
			}
		}

	}

	public static BigCustomerSubscribeFragment newInstance(InterFaceBigCustomerOrderBeanAble bean) {
		BigCustomerSubscribeFragment test = new BigCustomerSubscribeFragment();
		test.setArguments(new Bundle());
		test.setData(bean);
		return test;

	}

	public void startUser() {
		showBuilder("开始用车", "请输入6位数用车密码", 2);
	}

	@Override
	public int setContentView() {
		return R.layout.dakehuyuyueframent;
	}

	@Override
	public void initView() {
		name = (TextView) findViewById(R.id.name);
		time_text = (TimerTaskTextView) findViewById(R.id.time);
		quxiao = (TextView) findViewById(R.id.close);
		xunche = (TextView) findViewById(R.id.xunche);
		framelayout = (LinearLayout) findViewById(R.id.framelayout);
	}

	@Override
	public void initData() {
		name.setText(bean.car_number);
		time_text.startTimerTask(bean.subscribeOrderExpire);
		time_text.setStopTimerTask(new StopTimerTask() {

			@Override
			public void stopTimer() {
				CustomHeightBean bean = new CustomHeightBean();
				bean.flag = MainActivityInterFace.CALLCLE_SUBSCRIBE_BIGCUSTOM;
				MyAppcation.getMyAppcation()
						.notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, bean);
			}

			@Override
			public void startTimer() {

			}
		});

	}

	@Override
	public void initEvent() {
		framelayout.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
		xunche.setOnClickListener(this);
		quxiao.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.close:
			showBuilder("温馨提示", "确定取消预约吗？", 1);
			break;
		case R.id.xunche:
			seek_carRequest();
			break;
		default:
			break;
		}
	}

	/**
	 * 取消预约
	 */

	private void cancelyuyue() {

		Cancel_subscribeRequest requestBean = new Cancel_subscribeRequest();
		requestBean.car_id = bean.car_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				bean.subscribeOrderExpire = 0;
				setData(null);
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	/**
	 * 
	 * @param message提示消息
	 * @param flag操作类型
	 *            ，0是免费预约次数用完1是取消预约2立即用车
	 */
	protected void showBuilder(String title, String message, int flag) {

		if (flag != 2) {
			mydialog = new TipLeftRightDialog(getActivity(), title, message, flag);
			((TipLeftRightDialog) mydialog).setDialogInterFace("取消", "确定", new DialogInterFace() {
				@Override
				public void right(int flag, Object t) {
					switch (flag) {
					case 0:
						mydialog.dismiss();
						showBuilder("开始用车", "请输入6位数用车密码", 2);
						break;
					case 1:
						mydialog.dismiss();
						cancelyuyue();
						break;
					case 2:
						mydialog.dismiss();
						userCar(t.toString());
						break;
					default:
						break;
					}
				}

				@Override
				public void left(int flag, Object t) {
					mydialog.dismiss();
				}
			}).show();
		} else {
			mydialog = new PasswordDialog(getActivity(), title, message, flag);
			((PasswordDialog) mydialog).setDialogInterFace("取消", new DialogInterFace() {

				@Override
				public void right(int flag, Object t) {

					switch (flag) {
					case 0:
						mydialog.dismiss();
						showBuilder("开始用车", "请输入6位数用车密码", 2);
						break;
					case 1:
						mydialog.dismiss();
						cancelyuyue();
						break;
					case 2:
						mydialog.dismiss();
						userCar(t.toString());
						break;

					default:
						break;
					}
				}

				@Override
				public void left(int flag, Object t) {
					mydialog.dismiss();
				}
			}).show();

		}

	}

	/**
	 * @param string
	 * 
	 */
	protected void userCar(String string) {

		Big_customer_rent_carRequest requestBean = new Big_customer_rent_carRequest();
		requestBean.user_pin = Des4.encode(string);
		requestBean.car_id = bean.car_id;
		GetDataInterFace<String> listener = new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				mydialog.dismiss();
				setData(null);
				BigCustomerOrderBean bean = JSON.parseObject(response, BigCustomerOrderBean.class);
				CustomHeightBean heightBean = new CustomHeightBean();
				heightBean.flag = MainActivityInterFace.START_USE_BIGCUSTOM_CAR;
				heightBean.data = bean;
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE,
						heightBean);

			}
		};
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, listener, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	private void seek_carRequest() {
		Seek_carRequest requestBean = new Seek_carRequest();
		requestBean.car_id = bean.car_id;
		requestBean.type = SeekCarCode.BIGCUSTOMER_TYPE;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				Seek_carResponse bean = JSON.parseObject(response, Seek_carResponse.class);
				bean.type = 3;
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.SEARCH_CAR_CODE,
						bean);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

}
